layui.use(['jquery', 'authtree', 'form', 'layer'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var authtree = layui.authtree;

    //获取路径参数
    var request = GetRequest();

    // 初始化菜单权限数据
    $.ajax({
        url: '/menu/tree',
        success: function(data){
            //处理返回的data树形数据
            var jsonStr = JSON.stringify(data.data);
            var menuStr = jsonStr.replace(/children/g,"list");
            var menuTree = JSON.parse(menuStr);
            // 渲染时传入渲染目标ID，树形结构数据（具体结构看样例，checked表示默认选中），以及input表单的名字
            authtree.render('#LAY-auth-tree-index', menuTree, {
                inputname: 'menuname',
                layfilter: 'lay-check-auth',
                openall: true,
                autowidth: true,
            });

            //回显角色信息和权限信息
            $.get('/role/' + request.id,function (data) {
                $("#roleId").val(data.id);
                $("#name").val(data.name);
                $("#enname").val(data.enname);
                $("#useable").val(data.useable);
                if (data.useable == "0") {
                    $("#useable").prop("checked",true);
                } else {
                    $("#useable").prop("checked",false);
                }
                $.each(data.menuIds,function (i, ele) {
                    $('input[value=' + ele + ']').prop('checked', 'checked');
                });
                //重新渲染
                form.render('checkbox');
                form.render();
            });
        }
    });

    //监听是否启用角色开关
    form.on('switch(switchTest)', function(data){
        if (this.checked) {
            $("#useable").val("0")
        } else {
            $("#useable").val("1")
        }
    });


    form.on("submit(edit)",function(data){
        var menuIds = authtree.getChecked('#LAY-auth-tree-index');
        //对data增加数组数据，使得数据的格式副歌springmvc的数组绑定格式
        for (var i = 0;i < menuIds.length;i++) {
            var keyStr = "menuIds" + '['+ i +']';
            data.field[keyStr] = menuIds[i];
        }
        console.log(data)
        data.field.useable =  $("#useable").val();
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //弹出loading
        $.ajax({
            url: '/role/edit',
            type: 'POST',
            data:data.field,
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("修改成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("修改失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("修改失败！");
            }
        });
        return false;
    });

    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});